import org.junit.Test;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LambdaDemo {
    static class Student {
        String name;
        int score;

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getScore() {
            return score;
        }

        public void setScore(int score) {
            this.score = score;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", score=" + score +
                    '}';

        }
    }


    public static <E> List<E> filter(List<E> list, Predicate<E> predicate) {
        ArrayList<E> retList = new ArrayList<>();
        for (E e : list) {
            if (predicate.test(e)) {
                retList.add(e);
            }
        }
        return retList;
    }

    public static <T, R> List<R> map(List<T> list, Function<T, R> mapper) {
        ArrayList<R> retList = new ArrayList<>(list.size());
        for (T t : list) {
            retList.add(mapper.apply(t));
        }
        return retList;
    }

    public static void main(String[] args) {
        List<Student> list = Arrays.asList(new Student("tony", 100), new Student("tom", 99)
                , new Student("joy", 66), new Student("Alice", 66), new Student("cody", 60));
//        List<Student> filter = filter(list, t -> t.score > 88);
//        System.out.println(filter.toString());
//        List<String> map = map(list, Student::getName);
//        System.out.println(map);
//        Student ccc = new Student("ccc", 111);
//
//        BiConsumer<Student, String> c = Student::setName;
//        c.accept(ccc, "aaa");
//        System.out.println(ccc);
        // List<String> collect = list.stream().map(t -> t.name).collect(Collectors.toList());
//        List<Student> collect = list.stream().filter(t -> t.score > 65).sorted(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getName)).collect(Collectors.toList());
//
//
//        System.out.println(collect);

        Stream.generate(Math::random).limit(10).forEach(System.out::println);
        Stream.iterate(1, a -> a + 2).limit(100).forEach(System.out::println);
    }

    @Test
    public void test1() {
        List<Student> list = Arrays.asList(new Student("tony", 100), new Student("tom", 99)
                , new Student("joy", 66), new Student("Alice", 66), new Student("cody", 60));
        //匿名内部类
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return Integer.compare(o1.name.length(), o2.name.length());
            }
        });
        //lambda
        Collections.sort(list, Comparator.comparingInt(s -> s.score));
        System.out.println(list);
        List<String> nameList = list.stream().map(t -> t.name).collect(Collectors.toList());
        // Collections.sort(nameList, Comparator.comparingt(String::length));

        // 默认实现,按照首字母顺序排序
        nameList.sort(String::compareTo);

        // 按照名字长度,从短到长,从长到短;
        nameList.sort(Comparator.comparingInt(String::length).reversed());
        System.out.println(nameList);

    }

    @Test
    public void test2() {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("a", 2);
        map.put("b", 3);
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey() + entry.getValue());
        }
        map.forEach((s, i) -> {
            System.out.println(i);
        });
        Collection<Integer> values = map.values();
        for (Integer value : values) {
            System.out.println(value);
        }

//        map.merge("a", 1, Integer::sum);
//        System.out.println(map);
    }

    @Test
    public void test3() {
        ArrayList<String> list = new ArrayList<>();

        list.add("1");
        list.add("2");
        list.removeIf(s -> "2".equals(s));
        System.out.println(list);

    }

    @Test
    public void test4() {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 4, 5));
        HashSet<Integer> integers = new HashSet<>(list);
        System.out.println(integers);
        System.out.println(list);
        list.removeAll(Collections.singleton(1));
        System.out.println(list);
        LocalDate now = LocalDate.now();

        now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(now);
    }


}
